package com.codeoss.framework.view.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Page;
import com.codeoss.framework.view.pagebean.base.BasePageBean;
import com.codeoss.framework.view.service.IPageComponentService;

@Service
public class PageComponentService extends ContainerService implements IPageComponentService,ApplicationContextAware{
    private ApplicationContext applicationContext;
	@Override
	public void init(YunContext yunContext, Page page) {
		// TODO Auto-generated method stub
		String pageBean=page.getPageBean();
		Map<String,Object> dataMap=new HashMap<String,Object>();
		if(!StringUtils.isEmpty(pageBean)) {
			BasePageBean basePageBean=(BasePageBean)applicationContext.getBean(pageBean);
			yunContext.setPageBean(basePageBean);
			processValue(yunContext, page,yunContext.getQueryMap(),dataMap);
			basePageBean.beforeInit();
			basePageBean.init();
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

	@Override
	public void processRequest(YunContext yunContext, Page page) {
		// TODO Auto-generated method stub
		String pageBean=page.getPageBean();
		Map<String,Object> dataMap=new HashMap<String,Object>();
		if(!StringUtils.isEmpty(pageBean)) {
			BasePageBean basePageBean=(BasePageBean)applicationContext.getBean(pageBean);
			yunContext.setPageBean(basePageBean);
			processValue(yunContext, page,yunContext.getViewRequest().getData(),dataMap);
			basePageBean.beforeInit();
		}
	}
	
	public void processValue(YunContext yunContext, Component component,Map<String,String> paraMap,Map<String,Object> dataMap) {
		super.processValue(yunContext, component, paraMap, dataMap);
	}
}
